#pragma once

#include "./render_grid.hpp"
#include "../tools/setting_interface.h"
#include "../ui_message/message.h"
#include "../utils/shader.h"

namespace qbe::axis {
void render_grid_init(entt::registry& reg, ecs::Query query) {
  auto& render_grid_model = query.get_resource<RenderGridModel>();
  render_grid_model.transform = vsg::MatrixTransform::create();
  auto view = reg.view<render::SceneTag, vsg::ref_ptr<vsg::Group>>();
  for (auto [e, sence] : view.each()) {
    auto options = query.get_resource<vsg::ref_ptr<vsg::Options>>();
    {

      utils::TempRender::GridShaderMaterial2D material{
          utils::TempRender::Grid_Uniform{vsg::vec2(0.0, 0.0), vsg::vec2(0.0, 0.0), vsg::vec4(0.0, 0.0, 0.0, 1.0), vsg::vec4(1.0, 1.0, 1.0, 1.0),
                                          vsg::vec4(1.0, 1.0, 1.0, 1.0), 1316, 976, 10, 1.0, 1, 1},
          "shaders/axis/grid.vert.spv", "shaders/axis/grid.frag.spv"};
      render_grid_model.grid_node = utils::TempRender::GridNode::create(options, material);
      sence->addChild(render_grid_model.transform);
      render_grid_model.transform->addChild(render_grid_model.grid_node);
    }
  }
}
void render_grid_update(ecs::Query query, ecs::Bus& bus) {
  auto& axis = query.get_resource<axis::Axis>();
  auto& render_grid_model = query.get_resource<RenderGridModel>();

  if (render_grid_model.is_init == false) {
    //初始化设置
    auto& setting_interface_object = query.get_resource<tools::SettingInterfaceObjcet>();
    auto setting_info = setting_interface_object.getDisplaySettingsInfo();

    axis.adsorp_precision = setting_info.drawing_board_adsorption_accuracy;

    double r, g, b, a;
    QColor color(setting_info.drawing_board_background);
    color.getRgbF(&r, &g, &b, &a);
    render_grid_model.m_bg_color = vsg::vec4(float(r), float(g), float(b), float(a));

    QColor color_1(setting_info.drawing_board_grid_color);
    color_1.getRgbF(&r, &g, &b, &a);
    render_grid_model.m_grid_color = vsg::vec4(float(r), float(g), float(b), float(a));
    render_grid_model.m_show_grid_type = setting_info.drawing_board_grid_type.toStdString();
    render_grid_model.is_init = true;
  }

  auto ui_messages = bus.receive<qbe::UiMessage::UiSetting>();
  for (auto mes : ui_messages) {
    double r, g, b, a;
    QColor color(mes.drawing_board_background);
    color.getRgbF(&r, &g, &b, &a);
    render_grid_model.m_bg_color = vsg::vec4(float(r), float(g), float(b), float(a));

    QColor color_1(mes.drawing_board_grid_color);
    color_1.getRgbF(&r, &g, &b, &a);
    render_grid_model.m_grid_color = vsg::vec4(float(r), float(g), float(b), float(a));
    render_grid_model.m_show_grid_type = mes.drawing_board_grid_type.toStdString();
  }

  auto t_matrix = vsg::translate(vsg::vec3(-axis.width / 2, -axis.height / 2, 0.0));
  render_grid_model.transform->matrix = t_matrix;
  // *width = window_resize_event->width + 16;
  // *height = window_resize_event->height + 39;
  if (render_grid_model.m_show_grid_type == "none")
    render_grid_model.grid_node->setGridParam(axis.width, axis.height, vsg::dvec2(axis.dx, double(axis.height) - axis.dy),
                                              axis.grid.real_width * axis.scale, render_grid_model.m_bg_color, render_grid_model.m_grid_color, false,
                                              false);
  else if (render_grid_model.m_show_grid_type == "Grid")
    render_grid_model.grid_node->setGridParam(axis.width, axis.height, vsg::dvec2(axis.dx, double(axis.height) - axis.dy),
                                              axis.grid.real_width * axis.scale, render_grid_model.m_bg_color, render_grid_model.m_grid_color, true,
                                              false);
  else if (render_grid_model.m_show_grid_type == "lattice")
    render_grid_model.grid_node->setGridParam(axis.width, axis.height, vsg::dvec2(axis.dx, double(axis.height) - axis.dy),
                                              axis.grid.real_width * axis.scale, render_grid_model.m_bg_color, render_grid_model.m_grid_color, false,
                                              true);
  UiMessage::DisplayMouseCoorMessage mes;
  mes.x = axis.real_x;
  mes.y = axis.real_y;
  bus.send(mes);
}
void RenderGridPlugin::build(ecs::App& app) {
  app.init_resource<RenderGridModel>(RenderGridModel{});

  app.add_systems<ecs::stage::PostStartup>(render_grid_init);
  app.add_systems<ecs::stage::Update>(render_grid_update);
}

}  // namespace qbe::axis